package com.school.graduation.goods.model;


import com.school.graduation.R;
import com.school.graduation.bean.GoodsBean;
import com.school.graduation.internet.GetGoodsInterface;
import com.school.graduation.model.Goods;
import com.school.graduation.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class GoodsFragmentModel implements ImplGoodsFragmentModel{

    private static GoodsFragmentModel model;
    private static byte[] bytes = new byte[1024];
    private List<String> urls;          //轮播图片的url集合
    private List<HotMarketItem> hots;   //热门市场对象集合
    private static List<GoodsBean> goods;

    private GoodsFragmentModel(){
        urls = new ArrayList<>();
        hots = new ArrayList<>();
        goods = new ArrayList<>();
    }

    public static GoodsFragmentModel getInstance(){
        if (model==null){
            synchronized (bytes){
                model = new GoodsFragmentModel();
                return model;
            }
        }
        return model;
    }

    /*
    获取热门市场数据
     */
    @Override
    public void getHotMarkets(final GetModelListener listener){
        hots.clear();
        HotMarketItem phone = new HotMarketItem();
        phone.setTitle("手机");
        phone.setResource(R.mipmap.phone);
        phone.setContent("低价玩转潮新机");
        HotMarketItem camera = new HotMarketItem();
        camera.setTitle("数码");
        camera.setResource(R.mipmap.camera);
        camera.setContent("二手低价淘尖货");
        HotMarketItem play = new HotMarketItem();
        play.setTitle("精美玩具");
        play.setResource(R.mipmap.play);
        play.setContent("好玩又开心");
        HotMarketItem computer = new HotMarketItem();
        computer.setTitle("电脑");
        computer.setResource(R.mipmap.computer);
        computer.setContent("要性能更要划算");
        HotMarketItem washing = new HotMarketItem();
        washing.setTitle("实用电器");
        washing.setResource(R.mipmap.washing);
        washing.setContent("大件小件省到家");
        HotMarketItem cloth = new HotMarketItem();
        cloth.setTitle("服装鞋帽");
        cloth.setResource(R.mipmap.cloth);
        cloth.setContent("闲置美衣转起来");
        hots.add(phone);
        hots.add(camera);
        hots.add(computer);
        hots.add(play);
        hots.add(washing);
        hots.add(cloth);
        listener.getHots(hots);
    }

    @Override
    public List<HotMarketItem> getHots(){
        return this.hots;
    }

    @Override
    public void getAllMsgFromNet(final GetModelListener listener) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://123.206.20.29:3000/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        GetGoodsInterface getGoods = retrofit.create(GetGoodsInterface.class);
        Call<List<GoodsBean>> call = getGoods.getAllGoods();
        call.enqueue(new Callback<List<GoodsBean>>() {
            @Override
            public void onResponse(Call<List<GoodsBean>> call, Response<List<GoodsBean>> response) {
                if (response.isSuccessful()&&listener!=null){
                    List<GoodsBean> goodsBeanList = response.body();
                    goods = goodsBeanList;
                    listener.getGoods(goods);
                }else {
                    listener.getError(response.errorBody().toString());
                }
            }

            @Override
            public void onFailure(Call<List<GoodsBean>> call, Throwable t) {
                listener.getError(t.toString());
            }
        });
    }
}
